/**
 * 表单参照选择页面
 */

mini.parse();
var grid = mini.get("datagrid1");
var grid2 = mini.get("datagrid2");
var value;

mini.get("sp").set({
	height : document.body.clientHeight - 40
});

function select(type) {
	value = mini.get('form'+type).getValue();
	console.log(value);
	var cancel;
	if(type == 'A') {
		cancel = mini.get('formB').getValue();
	}else {
		cancel = mini.get('formA').getValue();
	}

	mini.open({
		url : bootPATH + "../frame/frame1/pages/reference/selectFormRef.html",
		title : "选择表格列分组",
		width: 600,
		height: 430,
		onload: function () {       //弹出页面加载完成
			var iframe = this.getIFrameEl();
			var data = {value: value, cancel: cancel};
			iframe.contentWindow.SetData(data);
		},
		ondestroy: function (action) {  //弹出页面关闭前
			if (action == "ok") {       //如果点击“确定”
				var iframe = this.getIFrameEl();
				var data = iframe.contentWindow.onTableOk();
				data = mini.clone(data);
				console.log(data,'----');
				mini.get('form'+type).setValue(data.formListId);
				mini.get('form'+type).setText(data.formName);

				if(type == 'A'){
					$.ajax({
						url: ajaxUrl + "/formltems/findByFormListId.do",
						type: "post",
						dataType: "json",
						data: {
							formId: data.formListId
						},
						success: function(grid_data) {
							console.log(grid_data.data);
							grid.loading();
							grid.setData(grid_data.data);

						},
						error : function(data){
							// alert('获取失败');
						}
					});
				}
			}
		}
	})
}

var selectedA;
var selectedB;
function onSelectionChanged(e) {
	var fa = mini.get('formA').getValue();
	var fb = mini.get('formB').getValue();
	if(fb == '') {
		// grid.deselectAll();
		mini.alert("请选择目标单据");

	}else {
		console.log(e.selected);
		selectedA = grid.getSelected();
		$.ajax({
			url: ajaxUrl + "/formltems/findByFormListId.do",
			type: "post",
			dataType: "json",
			data: {
				formId: fb
			},
			success: function(grid_data) {
				console.log(grid_data.data);
				grid2.loading();
				grid2.setData(grid_data.data);
				// 默认选中
				grid2.findRows(function (row) {
					if (row.cTableField == e.selected.formBEn) {
						grid2.select(row);
					}
				});
			},
			error : function(data){
				// alert('获取失败');
			}
		});
	}
}

//设置目标字段
var flag;
function onFormBChanged(e) {
	if(e.selected != undefined && selectedA.formBEn != e.selected.cTableField) {
		var data = grid.getData();
		flag = true;
		for (var i = 0; i < data.length; i++) {

			if (data[i].formBEn == e.selected.cTableField && data[i].formBEn != selectedA.formBEn) {
				mini.alert('字段已被选中，请重新选择！');
				grid2.deselect(e.selected);
				// grid2.select(selectedA);
				flag = false;
				return;
			}

		}
	}
	if(flag) {
		grid.updateRow(selectedA, {formB: e.selected.cLadText,formBEn: e.selected.cTableField,formBId: e.selected.formList.formListId});
	}

}


function clear() {
	console.log(selectedA);
	if(selectedA) {
		grid.updateRow(selectedA, {formB: '',formBEn:''});
	}
}

function save() {
	console.log();
	var data = grid.getData();
	var save_data = [];
	for(var i=0;i<data.length;i++) {
		if(data[i].formBId != undefined) {
			save_data.push({
				sourceDocId: data[i].formList.formListId,//源单据id
				sourceField: data[i].cTableField,//源单据字段
				targetDocId: data[i].formBId,
				targetField: data[i].formBEn
			})
		}
	}
	$.ajax({
		url: ajaxUrl+"/referenceForm/insertReference.do",
		type: "post",
		dataType: "json",
		data: JSON.stringify(save_data),
		contentType : "application/json",
		success: function(grid_data) {
			mini.alert('保存成功！');

		},
		error : function(data){
			// alert('获取失败');
		}
	});

}